package com.kefu.boss.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.boss.mapper.BossMenuMapper;
import com.kefu.boss.model.BossMenu;
import com.kefu.common.service.AbstractKefuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class BossMenuService extends AbstractKefuService<BossMenu> {
    @Resource
    protected BossMenuMapper bossMenuMapper;
    public List<BossMenu> getMenuByRoleId(String roleId) {
        return bossMenuMapper.getMenuByRoleId(roleId);
    }

    public List<?> listTree() {
        List<BossMenu> dataList = bossMenuMapper.selectList(Wrappers.lambdaQuery(BossMenu.class));
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        for(BossMenu item : dataList){

            TreeNode<String> node = new TreeNode<>(item.getId(), item.getParentId(), item.getName(), item.getWeight());
            Map<String, Object> extra = new HashMap<>();

            extra.put("path", item.getPath());
            extra.put("component", item.getComponent());
            extra.put("redirect", item.getRedirect());
            extra.put("icon", item.getIcon());
            extra.put("type", item.getType());
            extra.put("hidden", true);

            node.setExtra(extra);

            nodeList.add(node);
        }
        return TreeUtil.build(nodeList, "0");
    }

    public Integer getMaxWeight(String parentId) {
        return bossMenuMapper.getMaxWeight(parentId);
    }
}
